﻿using System;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.DocumentFlowPages
{
    /// <summary>
    /// DefineStyle 的摘要说明。
    /// </summary>
    public partial class DefineStylePage : Page
    {
        private int _descriptionId;
        private int _positionId;
        private int _styleId;

        protected FlowStyle CurrentFlowStyle
        {
            get;
            private set;
        }

        protected override void OnInit(EventArgs e)
        {
            cmdAdd.Click += cmdAdd_Click;
            cmdUpdate.Click += cmdUpdate_Click;
            cmdDelete.Click += cmdDelete_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            _styleId = QSUtil.GetPositiveInt32("StyleID");
            _descriptionId = QSUtil.GetPositiveInt32("DescriptionID");
            _positionId = QSUtil.GetPositiveInt32("Position");
            if (_styleId <= 0)
            {
                SiteContext.Current.AlertAndRedirect("未指定表单", "ManageStyle.aspx");
                return;
            }

            CurrentFlowStyle = FlowStyle.GetFlowStyle(_styleId);
            if (CurrentFlowStyle == null)
            {
                SiteContext.Current.AlertAndRedirect("找不到指定的表单", "ManageStyle.aspx");
                return;
            }

            if (!Page.IsPostBack)
            {
                Bangding();
            }

            base.OnLoad(e);
        }

        private void Bangding()
        {
            if (_descriptionId > 0)
            {
                cmdUpdate.Enabled = true;
                cmdDelete.Enabled = true;
            }
            else
            {
                cmdUpdate.Enabled = false;
                cmdDelete.Enabled = false;
            }

            FillFieldName(ddlFieldName);
            var list = FlowStyleDescription.GetStyleDescription(_styleId);
            styleRepeater.DataSource = list;
            styleRepeater.DataBind();

            if (_descriptionId > 0)
            {
                foreach (var field in list)
                {
                    if (field.DescriptionID == _descriptionId)
                    {
                        GetValue(field);
                    }
                }
            }
        }

        private void GetValue(FlowStyleDescription field)
        {
            Debug.Assert(field != null);

            for (int i = 0; i < ddlFieldName.Items.Count; i++)
            {
                if (string.Compare(ddlFieldName.Items[i].Value, field.Name, true) == 0)
                {
                    ddlFieldName.SelectedIndex = i;
                }
            }


            tbFieldDescription.Text = field.Description;
            evaluateField.ChooseYes = field.Judged;
            multilineField.ChooseYes = field.Multiline;

            tbWidth.Text = field.Width.ToString();
            tbHeight.Text = field.Height.ToString();
            tbPosition.Text = field.Position.ToString();
            tbExample.Text = field.Example;
        }

        private static void FillFieldName(DropDownList ddl)
        {
            ddl.Items.Clear();
            for (int i = 1; i <= 26; i++)
            {
                string text = ((char)('a' + i - 1)).ToString();
                ddl.Items.Add(new ListItem(i.ToString(), text));
            }
        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (Page.IsValid)
            {
                if (FlowStyleDescription.AddStyleDescription(
                        _styleId,
                        ddlFieldName.SelectedItem.Value,
                        tbFieldDescription.Text,
                        evaluateField.ChooseYes,
                        multilineField.ChooseYes,
                        Int32.Parse(tbHeight.Text),
                        Int32.Parse(tbWidth.Text),
                        Int32.Parse(tbPosition.Text),
                        tbExample.Text) != 0)
                {
                    SiteContext.Current.Alert("所加的字段有重复！");
                }
                Bangding();
            }
        }

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            // if (FlowStyleDescription.UpdateStyleDescription(_descriptionId, ddlFieldName.SelectedItem.Value, tbFieldDescription.Text, evaluateField.ChooseYes, multilineField.ChooseYes, Int32.Parse(tbHeight.Text), Int32.Parse(tbWidth.Text), Int32.Parse(tbPosition.Text), tbExample.Text) != 0)
            // When update the style description, the following fields won't be updated:
            //  1. style id
            //  2. field name (a, b, c...)
            //
            if (FlowStyleDescription.UpdateStyleDescription(_descriptionId, tbFieldDescription.Text, evaluateField.ChooseYes, multilineField.ChooseYes, Int32.Parse(tbHeight.Text), Int32.Parse(tbWidth.Text), Int32.Parse(tbPosition.Text), tbExample.Text) != 0)
            {
                SiteContext.Current.Alert("所修改的字段有重复！");
            }
            Bangding();
        }

        private void cmdDelete_Click(object sender, EventArgs e)
        {
            FlowStyleDescription.DeleteStyleDescription(_descriptionId);

            _descriptionId = -1;
            tbFieldDescription.Text = "";
            tbWidth.Text = "450";
            tbHeight.Text = "20";
            tbPosition.Text = "1";
            Bangding();
        }
    }
}